package com.crazyteam.genid.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class GenPkThreadPoolFactory {

    @Autowired
    private GenPkThreadPoolConfig genPkThreadPoolConfig;


    @Bean("genpkThreadPool")
    public Executor getGenPkThreadPool(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor =  getExecutor();
        threadPoolTaskExecutor.setCorePoolSize(genPkThreadPoolConfig.getCorePoolSize());
        threadPoolTaskExecutor.setMaxPoolSize(genPkThreadPoolConfig.getMaxPoolSize());
        threadPoolTaskExecutor.setQueueCapacity(genPkThreadPoolConfig.getQueueCapacity());
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        return  threadPoolTaskExecutor ;
    }

    private ThreadPoolTaskExecutor getExecutor(){
        return new ThreadPoolTaskExecutor();
    }
}
